package com.sinosoft.cloud.access.service;


import com.alibaba.tamper.BeanMapping;
import com.alibaba.tamper.core.config.BeanMappingConfigHelper;
import com.sinosoft.cloud.access.annotations.AccessService;
import com.sinosoft.cloud.access.entity.*;
import com.sinosoft.cloud.access.net.AccessControl;
import com.sinosoft.cloud.access.util.CodeTransformUtil;
import com.sinosoft.cloud.access.util.TimeUtil;
import com.sinosoft.cloud.rest.TradeInfo;
import com.sinosoft.lis.entity.*;
import com.sinosoft.lis.pubfun.PubFun;
import com.sinosoft.utility.ExceptionUtils;
import com.sinosoft.utility.StrTool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public abstract class AccessServiceBean {

    /**
     * 日志管理
     */
    private final Log cLogger = LogFactory.getLog(this.getClass());
    /**
     * 错误处理类，每个需要错误处理的类中都放置该类
     */
//    public CErrors mErrors = new CErrors();

    /**
     * 若处理过程中出现异常信息，Head中的Flag字段所需要设置的值
     */
    private static final String ONE = "1";
    /**
     * 若出现异常信息，Head中的Desc字段所需要设置的值
     */
    private static final String ERROR_DESC = "内部错误，交易失败";
    /**
     * 若出现异常信息，记录日志的错误前缀
     */
    private static final String ERROR_INFO = "交易失败，错误信息：";

    /**
     * 创建由spring管理的读取配置文件对象
     */
    @Autowired
    AccessControl accessControl;


    @PostConstruct
    public void init(){
        AccessServiceGate accessServiceGate = AccessServiceGate.getInstance();
        AccessService accessService =  AnnotationUtils.findAnnotation(this.getClass(), AccessService.class);
        String name = accessService.name();
        String transCode = accessService.transCode();
        accessServiceGate.addService(name, transCode,this);
    }

    public TranData service(TranData msg) {

        long startTime = System.currentTimeMillis();

        cLogger.info(msg.getHead().getTransCode() + "交易开始！");
        //将TranData转换为TradeInfo
        TradeInfo tradeInfo = null;
        try {
            tradeInfo = tranTransInfo(msg);
        } catch (Exception e) {
            cLogger.error(ExceptionUtils.exceptionToString(e));
            return getErrorTranData(msg, ERROR_DESC);
        }
        cLogger.info("TranData转换为TradeInfo成功，返回结果是：" + tradeInfo);

        String errorStr = "";
        String log = "";

        errorStr = (String) tradeInfo.getData("return");
        log = (String) tradeInfo.getData("log");
        if (null != errorStr && !"".equals(errorStr)) {
            cLogger.warn(ERROR_INFO + log);
            return getErrorTranData(msg, errorStr);
        }

        if (tradeInfo.getErrorList().size() > 0) {
            cLogger.error(ERROR_INFO + tradeInfo.getErrorList().get(0));
            return getErrorTranData(msg, ERROR_DESC);
        }

        TradeInfo result = new TradeInfo();
        /**
         * 若配置中挡板配置为true，则不调用任何微服务，直接将封装的TradeInfo进行返回xsl转换
         */
        Boolean barrierControl = accessControl.getBarrierControl();
        if (barrierControl){
            result = tradeInfo;
        }else {
            result = service(tradeInfo);
        }

        errorStr = (String) result.getData("return");
        log = (String) result.getData("log");
        if (null != errorStr && !"".equals(errorStr)) {
            cLogger.warn(ERROR_INFO + log);
            return getErrorTranData(msg, errorStr);
        }

        if (result.getErrorList().size() > 0) {
            cLogger.error(ERROR_INFO + result.getErrorList().get(0));
            return getErrorTranData(msg, ERROR_DESC);
        }

        //将TradeInfo转换为TranData
        TranData tranData = null;
        try {
            tranData = transTranData(result, msg);
        } catch (Exception e) {
            cLogger.error(ExceptionUtils.exceptionToString(e));
            errorStr = "报文转换失败！";
            return getErrorTranData(msg, errorStr);
        }

        long endTime = System.currentTimeMillis();
        cLogger.info(TimeUtil.getMillis(startTime, endTime, msg.getHead().getTransCode() + "交易"));
        cLogger.info(msg.getHead().getTransCode() + "交易成功！交易结束。");
        return tranData;
    }

    /**
     * TranData转换为pojo，封装到tradeinfo
     *
     * @param msg
     * @return
     */
    private TradeInfo tranTransInfo(TranData msg) {

         /*标准报文中报文体信息*/
        Body body = null;
        /*标准报文中报文体中险种信息*/
        Risks risks = null;
        /*标准报文中报文体中投保人信息*/
        Appnt appnt = null;
        /*标准报文中报文体中被保人信息*/
        Insured insured = null;
        /*标准报文中报文体中受益人信息*/
        Bnfs bnfs = null;
        /*标准报文中报文头的信息*/
        Head head = null;
        /*标准报文中报文体中客户告知的信息*/
        CustomerImparts customerImparts = null;

        //获取TranData数据，将数据备份到局部变量中，若有错误，则返回交易失败
        TradeInfo inputData = getInputData(msg);
        if (inputData.getErrorList().size() > 0) {
            return inputData;
        }

        body = (Body) inputData.getData(Body.class.getName());
        risks = (Risks) inputData.getData(Risks.class.getName());
        appnt = (Appnt) inputData.getData(Appnt.class.getName());
        insured = (Insured) inputData.getData(Insured.class.getName());
        bnfs = (Bnfs) inputData.getData(Bnfs.class.getName());
        head = (Head) inputData.getData(Head.class.getName());
        customerImparts = (CustomerImparts) inputData.getData(CustomerImparts.class.getName());

        //将标准报文转换为pojo
        TradeInfo reqTradeInfo = stdToPojo(body, risks, appnt, insured, bnfs, head, customerImparts);

        return reqTradeInfo;
    }

    /**
     * 从tradeinfo中取pojo，转换为TranData
     *
     * @param respTradeInfo,msg
     * @return
     */
    private TranData transTranData(TradeInfo respTradeInfo, TranData msg) {

        msg = pojoToStd(respTradeInfo, msg);

        return msg;
    }

    /**
     * 从TranData获取所有对象，备份到局部变量
     *
     * @param cInputData
     * @return
     */
    private TradeInfo getInputData(TranData cInputData) {

        TradeInfo tradeInfo = new TradeInfo();

        Body body = cInputData.getBody();
        Risks risks = cInputData.getBody().getRisks();
        Appnt appnt = cInputData.getBody().getAppnt();
        Insured insured = cInputData.getBody().getInsured();
        Bnfs bnfs = cInputData.getBody().getBnfs();
        Head head = cInputData.getHead();
        CustomerImparts customerImparts = cInputData.getBody().getCustomerImparts();

        String errorStr = "";

        if (null == body) {
            errorStr = "接收到报文中的body数据异常";
            tradeInfo.addError(errorStr);
            return tradeInfo;
        }

        if (null == head) {
            errorStr = "接收到报文中Head数据异常";
            tradeInfo.addError(errorStr);
            return tradeInfo;
        }

        tradeInfo.addData(Body.class.getName(), body);
        tradeInfo.addData(Risks.class.getName(), risks);
        tradeInfo.addData(Appnt.class.getName(), appnt);
        tradeInfo.addData(Insured.class.getName(), insured);
        tradeInfo.addData(Bnfs.class.getName(), bnfs);
        tradeInfo.addData(Head.class.getName(), head);
        tradeInfo.addData(CustomerImparts.class.getName(), customerImparts);

        return tradeInfo;
    }

    /**
     * 将标准报文转换为pojo，封装到TradeInfo并返回
     *
     * @return
     */
    private TradeInfo stdToPojo(Body body, Risks risks, Appnt appnt, Insured insured, Bnfs bnfs, Head head, CustomerImparts customerImparts) {
        TradeInfo tradeInfo = new TradeInfo();

        //读取对象转换的配置信息
        BeanMappingConfigHelper.getInstance().registerConfig("mapping/request-mapping.xml");

        //1002数据转换
        tradeInfo.addData(LCContPojo.class.getName(), getCont(body, appnt, insured, head, risks));
        try {
            tradeInfo.addData(LCPolPojo.class.getName(), listPol(risks, body, head, insured));
        } catch (NumberFormatException e) {
            cLogger.error(ExceptionUtils.exceptionToString(e));
            tradeInfo.addData("return", e.getMessage());
            return tradeInfo;
        }

        tradeInfo.addData(LCAppntPojo.class.getName(), getAppnt(appnt, body, head));
        tradeInfo.addData(LCAddressPojo.class.getName(), listAddress(appnt, insured));
        tradeInfo.addData(LCInsuredPojo.class.getName(), listInsured(insured, appnt, customerImparts));
        try {
            tradeInfo.addData(LCBnfPojo.class.getName(), listBnf(bnfs));
        } catch (RuntimeException e) {
            cLogger.error(ExceptionUtils.exceptionToString(e));
            tradeInfo.addData("return", e.getMessage());
            return tradeInfo;
        }
        try {
            tradeInfo.addData(LCCustomerImpartPojo.class.getName(), listCustomerImpart(customerImparts, insured, body, head));
        } catch (NumberFormatException e) {
            cLogger.error(ExceptionUtils.exceptionToString(e));
            tradeInfo.addData("return", e.getMessage());
            return tradeInfo;
        } catch (RuntimeException e) {
            cLogger.error(ExceptionUtils.exceptionToString(e));
            tradeInfo.addData("return", e.getMessage());
            return tradeInfo;
        }

        tradeInfo.addData(LCAccountPojo.class.getName(), getAccount(head, body));
        tradeInfo.addData(LCSpecPojo.class.getName(), listSpec(body));
        tradeInfo.addData(LDPersonPojo.class.getName(), getPerson());
        tradeInfo.addData(LCAppntLinkManInfoPojo.class.getName(), getAppntLinkManInfoPojo());
        tradeInfo.addData(LCDutyPojo.class.getName(), listDuty());
        tradeInfo.addData(LCGetPojo.class.getName(), listGet());
        tradeInfo.addData(LCPremPojo.class.getName(), listPrem());
        tradeInfo.addData(LCCustomerImpartDetailPojo.class.getName(), listCustomerImpartDetail());
        tradeInfo.addData(LCCustomerImpartParamsPojo.class.getName(), listLCCustomerImpartParams());

        //1009数据转换
        tradeInfo.addData(LKTransStatusPojo.class.getName(), getLKTransStatus(body, head));

        //封装原始数据
        tradeInfo = getOtherInfo(tradeInfo, head, risks, body);

        return tradeInfo;
    }


    /**
     * 转换投保人信息
     *
     * @return
     */
    public LCAppntPojo getAppnt(Appnt appnt, Body body, Head head) {

        LCAppntPojo rLcAppntPojo = new LCAppntPojo();

        if (null != appnt) {
            BeanMapping.create(Appnt.class, LCAppntPojo.class).mapping(appnt, rLcAppntPojo);

            String appntValidityday = appnt.getAppntValidityday();
            if (!org.apache.commons.lang.StringUtils.isEmpty(appntValidityday) && appntValidityday.length() == 8) {
                rLcAppntPojo.setIdValiDate(TimeUtil.dateString8To10(appntValidityday));
            } else {
                rLcAppntPojo.setIdValiDate(appntValidityday);
            }

            rLcAppntPojo.setAppntBirthday(TimeUtil.dateString8To10(appnt.getBirthday()));
        }

        BeanMapping.create(Body.class, LCAppntPojo.class).mapping(body, rLcAppntPojo);

        return rLcAppntPojo;
    }

    /**
     * 转换受益人信息
     *
     * @return
     */
    public List<LCBnfPojo> listBnf(Bnfs bnfs) {
        ArrayList<LCBnfPojo> lcBnfPojos = new ArrayList<>();

        if (null == bnfs || null == bnfs.getBnfs()) {
            return lcBnfPojos;
        }

        List<Bnf> bnfList = bnfs.getBnfs();
        for (int i = 0; i < bnfList.size(); i++) {
            Bnf bnf = bnfList.get(i);
            LCBnfPojo rLcBnfPojo = new LCBnfPojo();

            BeanMapping.create(Bnf.class, LCBnfPojo.class).mapping(bnf, rLcBnfPojo);

            String bnfValidityday = bnf.getBnfValidityday();
            if (bnfValidityday != null && !bnfValidityday.equals("") && bnfValidityday.length() == 8) {
                rLcBnfPojo.setIdValiDate(TimeUtil.dateString8To10(bnfValidityday));
            } else {
                rLcBnfPojo.setIdValiDate(bnfValidityday);
            }

            rLcBnfPojo.setBirthday(TimeUtil.dateString8To10(bnf.getBirthday()));

            if (!org.apache.commons.lang.StringUtils.isEmpty(bnf.getLot())) { //受益份额
                try {
                    double bnfLot = Double.parseDouble(bnf.getLot().trim());
                    bnfLot /= 100;
                    rLcBnfPojo.setBnfLot(bnfLot);
                } catch (NumberFormatException e) {
                    throw new RuntimeException("受益份额必须为全数字");
                }
            } else {
                rLcBnfPojo.setBnfLot(-1);
            }
            if (org.apache.commons.lang.StringUtils.isEmpty(bnf.getGrade())) {
                rLcBnfPojo.setBnfNo(Integer.MAX_VALUE);
            } else {
                try {
                    rLcBnfPojo.setBnfNo(bnf.getGrade());
                } catch (NumberFormatException e) {
                    throw new RuntimeException("受益顺序必须为全数字");
                }
            }
            if (null != bnf.getAddress() && !"".endsWith(bnf.getAddress().trim()) &&
                    !"null".equals(bnf.getAddress().trim())) {
                rLcBnfPojo.setAddress("3~" + bnf.getAddress());
            }
            lcBnfPojos.add(rLcBnfPojo);
        }

        return lcBnfPojos;
    }

    /**
     * 转换地址信息信息
     *
     * @return
     */
    public List<LCAddressPojo> listAddress(Appnt appnt, Insured insured) {

        List<LCAddressPojo> lcAddressPojos = new ArrayList<LCAddressPojo>();
        //投保人地址信息
        LCAddressPojo appLcAddressPojo = new LCAddressPojo();

        if (null != appnt) {
            BeanMapping.create(Appnt.class, LCAddressPojo.class).mapping(appnt, appLcAddressPojo);

            if (appnt.getPhone().indexOf("-") > -1) {
                String[] phone = appnt.getPhone().split("-", 2);
                appLcAddressPojo.setZoneCode(phone[0]);
                appLcAddressPojo.setPhone(phone[1]);
            } else {
                appLcAddressPojo.setPhone(appnt.getPhone());
            }
            lcAddressPojos.add(appLcAddressPojo);
        }

        //被保人地址信息
        LCAddressPojo insuLcAddressPojo = new LCAddressPojo();

        if (null != insured) {
            BeanMapping insuredMapping = BeanMapping.create(Insured.class, LCAddressPojo.class);
            insuredMapping.mapping(insured, insuLcAddressPojo);

            if (insured.getPhone().indexOf("-") > -1) {
                String[] phone = insured.getPhone().split("-", 2);
                insuLcAddressPojo.setZoneCode(phone[0]);
                insuLcAddressPojo.setPhone(phone[1]);
            } else {
                insuLcAddressPojo.setPhone(insured.getPhone());
            }
            lcAddressPojos.add(insuLcAddressPojo);
        }

        return lcAddressPojos;
    }

    /**
     * 转换被保人信息
     *
     * @return
     */
    public List<LCInsuredPojo> listInsured(Insured insured, Appnt appnt, CustomerImparts customerImparts) {

        LCInsuredPojo rLcInsuredPojo = new LCInsuredPojo();

        if (null != insured) {
            BeanMapping insuredMapping = BeanMapping.create(Insured.class, LCInsuredPojo.class);
            insuredMapping.mapping(insured, rLcInsuredPojo);

            String insuValidityday = insured.getInsValidityday();
            if (!org.apache.commons.lang.StringUtils.isEmpty(insuValidityday) && insuValidityday.length() == 8) {
                rLcInsuredPojo.setIdValiDate(TimeUtil.dateString8To10(insuValidityday));
            } else {
                rLcInsuredPojo.setIdValiDate(insuValidityday);
            }
            rLcInsuredPojo.setBirthday(TimeUtil.dateString8To10(insured.getBirthday()));
        }

        if (null != appnt && null != insured) {
            rLcInsuredPojo.setRelationToAppnt(CodeTransformUtil.appRela2InsuRela(appnt.getRelaToInsured(), insured.getSex()));
        }
        if (null != customerImparts && null != customerImparts.getCustomerImparts()
                && customerImparts.getCustomerImparts().size() > 0) {
            try {
                rLcInsuredPojo.setStature(customerImparts.getCustomerImparts().get(4).getImpartParamModle());
                rLcInsuredPojo.setAvoirdupois(customerImparts.getCustomerImparts().get(5).getImpartParamModle());
            } catch (NumberFormatException e) {
                return null;
            }
        }

        List<LCInsuredPojo> lcInsuredPojos = new ArrayList<>();
        lcInsuredPojos.add(rLcInsuredPojo);
        return lcInsuredPojos;
    }

    /**
     * 转换合同信息
     *
     * @return
     */
    public LCContPojo getCont(Body body, Appnt appnt, Insured insured, Head head, Risks risks) {

        //需要转化的LCContPojo对象
        LCContPojo rLcContPojo = new LCContPojo();

        BeanMapping.create(Body.class, LCContPojo.class).mapping(body, rLcContPojo);

        if (null != appnt) {
            BeanMapping.create(Appnt.class, LCContPojo.class).mapping(appnt, rLcContPojo);
            rLcContPojo.setAppntBirthday(TimeUtil.dateString8To10(appnt.getBirthday()));
        }

        if (null != insured) {
            BeanMapping.create(Insured.class, LCContPojo.class).mapping(insured, rLcContPojo);
            rLcContPojo.setInsuredBirthday(TimeUtil.dateString8To10(insured.getBirthday()));
        }

        BeanMapping.create(Head.class, LCContPojo.class).mapping(head, rLcContPojo);

        rLcContPojo.setPolApplyDate(TimeUtil.dateString8To10(body.getPolApplyDate()));
        rLcContPojo.setCValiDate(TimeUtil.getCValiDate(body.getPolApplyDate()));
        rLcContPojo.setCustomGetPolDate(TimeUtil.getCurrentDate());
        rLcContPojo.setFirstPayDate(TimeUtil.getCurrentDate());

        if (null != risks && null != risks.getRisks() && risks.getRisks().size() > 0) {
            rLcContPojo.setPayIntv(risks.getRisks().get(0).getPayIntv());
        }

        return rLcContPojo;
    }

    /**
     * 转换责任信息
     *
     * @return
     */
    public List<LCDutyPojo> listDuty() {
        return new ArrayList<LCDutyPojo>();
    }

    /**
     * 转换险种信息
     *
     * @return
     */
    public List<LCPolPojo> listPol(Risks risks, Body body, Head head, Insured insured) {
        List<LCPolPojo> lcPolPojos = new ArrayList<>();

        if (null == risks || null == risks.getRisks()) {
            LCPolPojo rlcPolPojo = new LCPolPojo();
            rlcPolPojo.setContNo(body.getContNo());
            rlcPolPojo.setRiskCode(body.getRiskCode());
            lcPolPojos.add(rlcPolPojo);
            return lcPolPojos;
        }

        if (null == risks) {
            return new ArrayList<LCPolPojo>();
        }

        List<Risk> riskList = risks.getRisks();
        int riskLength = riskList.size();
        for (int i = 0; i < riskLength; i++) {

            LCPolPojo rLcPolPojo = new LCPolPojo();

            BeanMapping.create(Risk.class, LCPolPojo.class).mapping(riskList.get(i), rLcPolPojo);

            BeanMapping.create(Head.class, LCPolPojo.class).mapping(head, rLcPolPojo);

            BeanMapping.create(Body.class, LCPolPojo.class).mapping(body, rLcPolPojo);

            if (null != insured) {
                BeanMapping.create(Insured.class, LCPolPojo.class).mapping(insured, rLcPolPojo);
            }

            if (org.apache.commons.lang.StringUtils.isEmpty(riskList.get(i).getPrem())) {
                rLcPolPojo.setPrem("0");
            } else {
                try {
                    rLcPolPojo.setPrem(String.valueOf(riskList.get(i).getPrem()));
                } catch (NumberFormatException e) {
                    cLogger.error(ExceptionUtils.exceptionToString(e));
                    throw new NumberFormatException("传入的险种保费不合法，应为纯数字！");
                }
            }

            if (org.apache.commons.lang.StringUtils.isEmpty(riskList.get(i).getAmnt())) {
                rLcPolPojo.setAmnt("0");
            } else {
                try {
                    rLcPolPojo.setAmnt(String.valueOf(riskList.get(i).getAmnt()));
                } catch (NumberFormatException e) {
                    cLogger.error(ExceptionUtils.exceptionToString(e));
                    throw new NumberFormatException("传入的险种保额不合法，应为纯数字！");
                }
            }

            if (org.apache.commons.lang.StringUtils.isEmpty(riskList.get(i).getMult())) {
                rLcPolPojo.setMult("0");
            } else {
                try {
                    rLcPolPojo.setMult(String.valueOf(riskList.get(i).getMult()));
                } catch (NumberFormatException e) {
                    cLogger.error(ExceptionUtils.exceptionToString(e));
                    throw new NumberFormatException("传入的险种份数不合法，应为纯数字！");
                }
            }


            if (org.apache.commons.lang.StringUtils.isEmpty(riskList.get(i).getPayIntv())) {
                rLcPolPojo.setPayIntv(-1);
            } else {
                try {
                    rLcPolPojo.setPayIntv(riskList.get(i).getPayIntv());
                } catch (NumberFormatException e) {
                    cLogger.error(ExceptionUtils.exceptionToString(e));
                    throw new NumberFormatException("传入的险种交费方式不合法，应为整数！");
                }
            }
            if ("5006".equals(rLcPolPojo.getRiskCode())) {
                rLcPolPojo.setPayIntv(0);
            }
            try {
                rLcPolPojo.setInsuYear(riskList.get(i).getInsuYear());
            } catch (NumberFormatException e) {
                cLogger.error(ExceptionUtils.exceptionToString(e));
                throw new NumberFormatException("传入的险种保险期间不合法，应为整数！");
            }
            try {
                rLcPolPojo.setGetYear(riskList.get(i).getGetYear());
            } catch (NumberFormatException e) {
                cLogger.error(ExceptionUtils.exceptionToString(e));
                throw new NumberFormatException("传入的险种领取年期不合法，应为整数！");
            }
            try {
                rLcPolPojo.setPayYears(riskList.get(i).getPayEndYear());
            } catch (NumberFormatException e) {
                cLogger.error(ExceptionUtils.exceptionToString(e));
                throw new NumberFormatException("传入的险种交费期间不合法，应为整数！");
            }
            try {
                rLcPolPojo.setRnewFlag(riskList.get(i).getRnewFlag());
            } catch (NumberFormatException e) {
                cLogger.error(ExceptionUtils.exceptionToString(e));
                throw new NumberFormatException("传入的险种自动续保标志不合法，应为整数！");
            }

            rLcPolPojo.setBonusGetMode(riskList.get(i).getBonusGetMode());
            rLcPolPojo.setEndDate(TimeUtil.dateString8To10(riskList.get(i).getEndDate()));

            if ("0".equals(riskList.get(i).getPayIntv())) {
                rLcPolPojo.setPayEndYear("1000");
                rLcPolPojo.setPayEndYearFlag("Y");
            } else {
                try {
                    rLcPolPojo.setPayEndYear(riskList.get(i).getPayEndYear());
                } catch (NumberFormatException e) {
                    cLogger.error(ExceptionUtils.exceptionToString(e));
                    throw new NumberFormatException("传入的险种交费期间不合法，应为整数！");
                }
            }

            String polApplyDate = body.getPolApplyDate();
            if (!org.apache.commons.lang.StringUtils.isEmpty(polApplyDate)) {
                rLcPolPojo.setCValiDate(TimeUtil.getCValiDate(polApplyDate));
            }

            if ("".equals(rLcPolPojo.getAutoPayFlag())) {
                rLcPolPojo.setAutoPayFlag("0");
            }

            if ("1".equals(rLcPolPojo.getLiveGetMode())) {
                rLcPolPojo.setLiveAccFlag("0");
            }
            if ("4".equals(rLcPolPojo.getLiveGetMode())) {
                rLcPolPojo.setLiveAccFlag("1");
            }

            if (!org.apache.commons.lang.StringUtils.isEmpty(rLcPolPojo.getRiskCode())) {
                lcPolPojos.add(rLcPolPojo);
            }
        }

        return lcPolPojos;
    }

    /**
     * 转换领取信息
     *
     * @return
     */
    public List<LCGetPojo> listGet() {
        return new ArrayList<LCGetPojo>();
    }

    /**
     * 转换给付信息
     *
     * @return
     */
    public List<LCPremPojo> listPrem() {
        return new ArrayList<LCPremPojo>();
    }

    /**
     * 转换LCCustomerImpart信息
     *
     * @return
     */
    public List<LCCustomerImpartPojo> listCustomerImpart(CustomerImparts customerImparts, Insured insured, Body body, Head head) {

        if (null == customerImparts || null == insured) {
            return new ArrayList<LCCustomerImpartPojo>();
        }

        List<CustomerImpart> impart = customerImparts.getCustomerImparts();


        List<LCCustomerImpartPojo> rCustomerImpartList = new ArrayList<>();

        String appntFamilyIncome = "";
        String appntIncome = "";

        //获取参数
        for (int i = 0; i < impart.size(); i++) {
            CustomerImpart impartInfo = impart.get(i);
            if ("dx0002".equals(impartInfo.getImpartObject())) {
                if ("lx003".equals(impartInfo.getImpartType())) {
                    if ("cw0001".equals(impartInfo.getImpartCode())) {
                        appntFamilyIncome = impartInfo.getImpartParamModle();
                    }
                    if ("cw0002".equals(impartInfo.getImpartCode())) {
                        appntIncome = impartInfo.getImpartParamModle();
                    }
                }
            }
        }

        if (org.apache.commons.lang.StringUtils.isEmpty(appntIncome)) {
            appntIncome = "*";
        }
        if (org.apache.commons.lang.StringUtils.isEmpty(appntFamilyIncome)) {
            appntFamilyIncome = "*";
        }

        //若投保人年收入和投保人家庭年收入若特别大，则会出现科学计数法，则需要转换为正常数字
        if (appntIncome.contains("e") || appntIncome.contains("E")) {
            BigDecimal bigDecimal = new BigDecimal(appntIncome);
            appntIncome = bigDecimal.toString();
        }
        if (appntFamilyIncome.contains("e") || appntFamilyIncome.contains("E")) {
            BigDecimal bigDecimal = new BigDecimal(Double.parseDouble(appntFamilyIncome));
            appntFamilyIncome = bigDecimal.toString();
        }

        if (!org.apache.commons.lang.StringUtils.isEmpty(appntFamilyIncome) && !org.apache.commons.lang.StringUtils.isEmpty(appntIncome)) {
            //投保人财务告知
            LCCustomerImpartPojo tLCCustomerImpart = new LCCustomerImpartPojo();
            //告知版别
            tLCCustomerImpart.setImpartVer("01");
            //告知编码
            tLCCustomerImpart.setImpartCode("001");
            //告知内容
            tLCCustomerImpart.setImpartContent("您每年固定收入    万元  主要收入来源： （序号）被选项：①工薪②个体③私营④房屋出租⑤证券投资⑥银行利息⑦其他 家庭年收入：");
            //印刷号码
            tLCCustomerImpart.setPrtNo("");
            //户号码类型 0 --客户号码为投保人 1 --客户号码为被保人 3 －客户号码为受益人
            tLCCustomerImpart.setCustomerNoType("0");
            tLCCustomerImpart.setImpartParamModle(appntIncome + ";" + "1" + ";" + appntFamilyIncome + ";");
            tLCCustomerImpart.setPatchNo("0");
            rCustomerImpartList.add(tLCCustomerImpart);
        }


        //被保人告知
        String insuredFamilyIncome = "";
        String insuredIncome = "";
        String insuredWeight = "";
        String insuredHeight = "";

        //获取参数
        for (int i = 0; i < impart.size(); i++) {
            CustomerImpart impartInfo = impart.get(i);
            if ("dx0003".equals(impartInfo.getImpartObject())) {
                if ("lx003".equals(impartInfo.getImpartType())) {
                    if ("cw0002".equals(impartInfo.getImpartCode())) {
                        insuredIncome = impartInfo.getImpartParamModle();
                    }
                } else if ("lx001".equals(impartInfo.getImpartType())) {
                    if ("jk0003".equals(impartInfo.getImpartCode())) {
                        insuredHeight = impartInfo.getImpartParamModle();
                    }
                    if ("jk0004".equals(impartInfo.getImpartCode())) {
                        insuredWeight = impartInfo.getImpartParamModle();
                    }

                    // 增加非实时出单出单的校验, 被保人有健康告知，请到保险公司投保
//                    if ("jk0002".equals(impartInfo.getImpartCode())
//                            && "Y".equals(impartInfo.getImpartParamModle())) {
//                        cLogger.error(insured.getName() + "：该被保人有健康告知，请到保险公司投保");
//                        CError tError = new CError();
//                        tError.functionName = "listCustomerImpart()";
//                        tError.errorMessage = insured.getName() + "：该被保人有健康告知，请到保险公司投保";
//                        mErrors.addOneError(tError);
//                        throw new RuntimeException("告知异常");
//                    }
//                    if ("jk0002".equals(impartInfo.getImpartCode())
//                            && "".equals(impartInfo.getImpartParamModle())) {
//                        throw new RuntimeException("被保险人健康告知不能为空，没有请注明无");
//                    }
//                    if ("jk0002".equals(impartInfo.getImpartCode())
//                            && null == impartInfo.getImpartParamModle()) {
//                        throw new RuntimeException("被保险人健康告知不能为空，没有请注明无");
//                    }
                }
            }

            if ("dx0002".equals(impartInfo.getImpartObject())) {
                if ("lx003".equals(impartInfo.getImpartType())) {
                    if ("cw0001".equals(impartInfo.getImpartCode())) {
                        insuredFamilyIncome = impartInfo.getImpartParamModle();
                    }
                }
            }
        }

        //被保人财务告知
        LCCustomerImpartPojo tFinanceImpart = new LCCustomerImpartPojo();
        //告知版别
        tFinanceImpart.setImpartVer("01");
        //告知编码
        tFinanceImpart.setImpartCode("001");
        //告知内容
        tFinanceImpart.setImpartContent("您每年固定收入    万元  主要收入来源： （序号）被选项：①工薪②个体③私营④房屋出租⑤证券投资⑥银行利息⑦其他 家庭年收入：");
        //印刷号码
        tFinanceImpart.setPrtNo("");
        //客户号码类型 0 --客户号码为投保人 1 --客户号码为被保人 3 －客户号码为受益人
        tFinanceImpart.setCustomerNoType("1");
        if (org.apache.commons.lang.StringUtils.isEmpty(insuredIncome)) {
            insuredIncome = "*";
        }
        if (org.apache.commons.lang.StringUtils.isEmpty(insuredFamilyIncome)) {
            insuredFamilyIncome = "*";
        }
        tFinanceImpart.setImpartParamModle(insuredIncome + ";" + "1" + ";" + insuredFamilyIncome + ";");
        tFinanceImpart.setPatchNo("0");
        rCustomerImpartList.add(tFinanceImpart);

        //被保人的身高体重告知
        LCCustomerImpartPojo tHealthImpart = new LCCustomerImpartPojo();
        //告知版别
        tHealthImpart.setImpartVer("02");
        //告知编码
        tHealthImpart.setImpartCode("000");
        //告知内容
        tHealthImpart.setImpartContent("身高________cm（厘米）  体重________Kg （公斤）");
        //印刷号码
        tHealthImpart.setPrtNo("");
        //客户号码类型 0 --客户号码为投保人 1 --客户号码为被保人 3 －客户号码为受益人
        tHealthImpart.setCustomerNoType("1");

        //被保人赋值身高、体重
        LCInsuredPojo tLCInsuredPojo = new LCInsuredPojo();

        //身高校验
        try {
            tLCInsuredPojo.setStature(insuredHeight);
        } catch (Exception e) {
            cLogger.error(ExceptionUtils.exceptionToString(e));
            throw new NumberFormatException("传入的被保人" + insured.getName() + "身高不合法，应为纯数字");
        }
        //体重校验
        try {
            tLCInsuredPojo.setAvoirdupois(insuredWeight);
        } catch (Exception e) {
            cLogger.error(ExceptionUtils.exceptionToString(e));
            throw new NumberFormatException("传入的被保人" + insured.getName() + "体重不合法，应为纯数字");
        }

        //空值处理
        if (org.apache.commons.lang.StringUtils.isEmpty(insuredHeight)) {
            insuredHeight = "*";
        }
        if (org.apache.commons.lang.StringUtils.isEmpty(insuredWeight)) {
            insuredWeight = "*";
        }
        tHealthImpart.setImpartParamModle(insuredHeight + ";" + insuredWeight);
        tHealthImpart.setPatchNo("0");
        rCustomerImpartList.add(tHealthImpart);

        //被保险人健康告知
        String prtno = body.getProposalPrtNo();

        if (body.getProposalPrtNo().length() < 14 && "04".equals(head.getBankCode())) {
            prtno = StrTool.LCh(body.getProposalPrtNo(), "0", 13);
            prtno = StrTool.LCh(prtno, "8", 14);
        }


        if (prtno != null && !"".equals(prtno) && "9".equals(prtno.substring(0, 1))) {
            LCCustomerImpartPojo tHealthImport2 = new LCCustomerImpartPojo();
            //告知版别
            tHealthImport2.setImpartVer("GX10");
            //告知编码
            tHealthImport2.setImpartCode("0");
            //告知内容
            tHealthImport2.setImpartContent("被保险人是否有下列情形之一？1智能障碍等2恶性肿瘤等3从事以下职业等①是②否");
            //客户号码类型 0 --客户号码为投保人 1 --客户号码为被保人 3 －客户号码为受益人
            tHealthImport2.setCustomerNoType("1");
            tHealthImport2.setImpartParamModle("2;");
            tHealthImport2.setPatchNo("19048");
            rCustomerImpartList.add(tHealthImport2);
        } else if (prtno != null && !"".equals(prtno) && "4".equals(prtno.substring(0, 1))) {
            LCCustomerImpartPojo tHealthImport2 = new LCCustomerImpartPojo();
            //告知版别
            tHealthImport2.setImpartVer("GX10");
            //告知编码
            tHealthImport2.setImpartCode("A");
            //告知内容
            tHealthImport2.setImpartContent("A1 智能障碍、失明、聋且哑、一肢以上肢体缺失或功能丧失。①是②否");
            //客户号码类型 0 --客户号码为投保人 1 --客户号码为被保人 3 －客户号码为受益人
            tHealthImport2.setCustomerNoType("1");
            tHealthImport2.setImpartParamModle("2;");
            tHealthImport2.setPatchNo("19057");
            rCustomerImpartList.add(tHealthImport2);


            LCCustomerImpartPojo tHealthImport3 = new LCCustomerImpartPojo();
            //告知版别
            tHealthImport3.setImpartVer("GX10");
            //告知编码
            tHealthImport3.setImpartCode("B");
            //告知内容
            tHealthImport3.setImpartContent("B1 类告知①是②否");
            //客户号码类型 0 --客户号码为投保人 1 --客户号码为被保人 3 －客户号码为受益人
            tHealthImport3.setCustomerNoType("1");
            tHealthImport3.setImpartParamModle("2;");
            tHealthImport3.setPatchNo("19057");
            rCustomerImpartList.add(tHealthImport3);
        }

        return rCustomerImpartList;
    }

    /**
     * 转换账户信息
     *
     * @return
     */
    public LCAccountPojo getAccount(Head head, Body body) {
        LCAccountPojo rLcAccountPojo = new LCAccountPojo();

        BeanMapping.create(Head.class, LCAccountPojo.class).mapping(head, rLcAccountPojo);

        BeanMapping.create(Body.class, LCAccountPojo.class).mapping(body, rLcAccountPojo);

        return rLcAccountPojo;
    }

    /**
     * 转换特别约定信息
     *
     * @return
     */
    public List<LCSpecPojo> listSpec(Body body) {
        ArrayList<LCSpecPojo> lcsPecPojos = new ArrayList<>();

        LCSpecPojo rLcsPecPojo = new LCSpecPojo();

        if (!org.apache.commons.lang.StringUtils.isEmpty(body.getSpecContent()) && !"N".equals(body.getSpecContent())) {
            rLcsPecPojo.setSpecContent(body.getSpecContent());
            lcsPecPojos.add(rLcsPecPojo);
        }

        return lcsPecPojos;
    }

    /**
     * 转换客户信息
     *
     * @return
     */
    public List<LDPersonPojo> getPerson() {
        ArrayList<LDPersonPojo> ldPersonPojos = new ArrayList<>();

        return ldPersonPojos;
    }

    /**
     * 转换连身投保人信息
     *
     * @return
     */
    public LCAppntLinkManInfoPojo getAppntLinkManInfoPojo() {
        return new LCAppntLinkManInfoPojo();
    }

    /**
     * 转换客户告知详情信息
     *
     * @return
     */
    public List<LCCustomerImpartDetailPojo> listCustomerImpartDetail() {
        ArrayList<LCCustomerImpartDetailPojo> lcCustomerImpartDetailPojos = new ArrayList<>();
        return lcCustomerImpartDetailPojos;
    }

    /**
     * 转换客户告知信息
     *
     * @return
     */
    public List<LCCustomerImpartParamsPojo> listLCCustomerImpartParams() {
        List<LCCustomerImpartParamsPojo> lcCustomerImpartParamsPojoList = new ArrayList<LCCustomerImpartParamsPojo>();

        return lcCustomerImpartParamsPojoList;
    }

    /**
     * 需要转换的其他数据
     */
    public TradeInfo getOtherInfo(TradeInfo tradeInfo, Head head, Risks risks, Body body) {
        //1002需要传的原始数据
        String zoneNo = head.getZoneNo();
        String brNo = head.getNodeNo();
        String mainRiskCode = "";
        String transNo = head.getTranNo();
        if (risks != null && risks.getRisks().size() > 0) {
            mainRiskCode = risks.getRisks().get(0).getMainRiskCode();
        }

        tradeInfo.addData("TransNo", transNo);
        tradeInfo.addData("autoRenew", "-9");
        GlobalPojo globalPojo = new GlobalPojo();

        BeanMapping.create(Head.class, GlobalPojo.class).mapping(head, globalPojo);

        globalPojo.setContPrtNo(body.getContPrtNo());
        globalPojo.setAgentPersonCode(body.getAgentPersonCode());
        globalPojo.setZoneNo(zoneNo);
        globalPojo.setBrNo(brNo);
        globalPojo.setMainRiskCode(mainRiskCode);
        tradeInfo.addData(GlobalPojo.class.getName(), globalPojo);
        tradeInfo.addData("payPrem", body.getPayPrem());
        tradeInfo.addData("otherNo", body.getContNo());
        tradeInfo.addData("applySerial", body.getAppNo());
        tradeInfo.addData("otherCompanyDieAmnt", body.getOtherCompanyDieAmnt());
        tradeInfo.addData("healthNotice",body.getHealthNotice());

        return tradeInfo;
    }

    /**
     * 将返回的pojo转换为TranData
     */
    public TranData pojoToStd(TradeInfo respTradeInfo, TranData msg) {

        LCContPojo respLCContPojo = (LCContPojo) respTradeInfo.getData(LCContPojo.class.getName());
        List<LMRiskPojo> respLMRiskList = (List<LMRiskPojo>) respTradeInfo.getData(LMRiskPojo.class.getName());
        List<LCPolPojo> respLCPolList = (List<LCPolPojo>) respTradeInfo.getData(LCPolPojo.class.getName());

        LCAppntPojo respLCAppntPojo = (LCAppntPojo) respTradeInfo.getData(LCAppntPojo.class.getName());
        List<LCAddressPojo> respLCAddressList = (List<LCAddressPojo>) respTradeInfo.getData(LCAddressPojo.class.getName());
        List<LCCustomerImpartParamsPojo> respLCCustomerImpartParamsList = (List<LCCustomerImpartParamsPojo>) respTradeInfo.getData(LCCustomerImpartParamsPojo.class.getName());

        List<LCInsuredPojo> respLCInsuredList = (List<LCInsuredPojo>) respTradeInfo.getData(LCInsuredPojo.class.getName());
        List<LCBnfPojo> respLCBnfList = (List<LCBnfPojo>) respTradeInfo.getData(LCBnfPojo.class.getName());
        List<LCSpecPojo> respLCSpecList = (List<LCSpecPojo>) respTradeInfo.getData(LCSpecPojo.class.getName());

        LAAgentPojo respLAAgentPojo = (LAAgentPojo) respTradeInfo.getData(LAAgentPojo.class.getName());
        LAAgent1Pojo respLAAgentPojo1 = (LAAgent1Pojo) respTradeInfo.getData(LAAgent1Pojo.class.getName());
        LAComPojo respLAComPojo = (LAComPojo) respTradeInfo.getData(LAComPojo.class.getName());
        LDComPojo respLDcomPojo = (LDComPojo) respTradeInfo.getData(LDComPojo.class.getName());
        List<CashValuePojo> respCashValueList = (List<CashValuePojo>) respTradeInfo.getData(CashValuePojo.class.getName());
        List<ReducedVolumePojo> respReducedVolumeList = (List<ReducedVolumePojo>) respTradeInfo.getData(ReducedVolumePojo.class.getName());
        List<LMRiskAppPojo> respLMRiskAppList = (List<LMRiskAppPojo>) respTradeInfo.getData(LMRiskAppPojo.class.getName());


        //1021所需要的对象
        msg.getBody().setContnoList(new ContnoList());
        msg.getBody().getContnoList().setContnoDetail(new ContnoDetail());
        msg.getBody().getContnoList().getContnoDetail().setRiskList(new RiskList());
        msg.getBody().getContnoList().getContnoDetail().setBnfList(new BnfList());
        msg.getBody().getContnoList().getContnoDetail().getRiskList().setRiskDetail(new RiskDetail());

        //创建risk对象
        msg.getBody().setRisk(new Risk());

        //创建risks对象
        msg.getBody().setRisks(new Risks());

        //创建PolicyBasicInfo对象
        msg.getBody().setPolicyBasicInfo(new PolicyBasicInfo());
        msg.getBody().getPolicyBasicInfo().setSpecialInfos(new SpecialInfos());

        //创建投保人对象
        msg.getBody().setAppnt(new Appnt());

        //创建被保人对象
        msg.getBody().setInsureds(new Insureds());

        //创建被保人对象
        msg.getBody().setBnfs(new Bnfs());


        BeanMappingConfigHelper.getInstance().registerConfig("mapping/response-mapping.xml");

        PolicyBasicInfo policyBasicInfo = new PolicyBasicInfo();
        if (null != respLCContPojo) {

            BeanMapping.create(LCContPojo.class, Body.class).mapping(respLCContPojo, msg.getBody());

            msg.getBody().setPrem(String.valueOf((respLCContPojo.getPrem())));
            msg.getBody().getRisk().setSignDate(TimeUtil.dateString10To8((respLCContPojo.getPolApplyDate())));

            BeanMapping.create(LCContPojo.class, PolicyBasicInfo.class).mapping(respLCContPojo, policyBasicInfo);

            policyBasicInfo.setPremText(PubFun.getChnMoney((respLCContPojo.getPrem())));
            policyBasicInfo.setSignDate(TimeUtil.dateString10To8(respLCContPojo.getSignDate()));//签单日期
            policyBasicInfo.setcValiDate(TimeUtil.dateString10To8(respLCContPojo.getCValiDate()));//生效日期
            policyBasicInfo.setContMakeDate(TimeUtil.dateFormat(respLCContPojo.getPolApplyDate()));//保险单制作日期
            policyBasicInfo.setContSignDate(TimeUtil.dateFormat(respLCContPojo.getSignDate()));//保险合同成立日期
            policyBasicInfo.setContValiDate(TimeUtil.dateFormat(respLCContPojo.getCValiDate()));//保险合同生效日期

            if (null != respLCContPojo.getSignDate() && null != respLCContPojo.getSignTime()) {
                policyBasicInfo.setFirstPremConfirmTime(TimeUtil.dateFormat(respLCContPojo.getSignDate()).concat(" ").concat(respLCContPojo.getSignTime().substring(0, 5)));//首期保险费收费确认时间
                policyBasicInfo.setValiContMakeTime(TimeUtil.dateFormat(respLCContPojo.getSignDate()).concat(" ").concat(respLCContPojo.getSignTime().substring(0, 5)));//有效保单生成时间
            }
        }
        if (null != respLAAgentPojo) {
            BeanMapping.create(LAAgentPojo.class, PolicyBasicInfo.class).mapping(respLAAgentPojo, policyBasicInfo);
        }
        if (null != respLAAgentPojo1) {
            policyBasicInfo.setBankWorkCardNo(respLAAgentPojo1.getQualNo()); //银行销售人员从业资格证编号
        }
        policyBasicInfo.setAgentWorkCardNo((String) respTradeInfo.getData("qualifNo"));//公司客户经理从业资格证编号
        if (null != respLAComPojo) {
            BeanMapping.create(LAComPojo.class, PolicyBasicInfo.class).mapping(respLAComPojo, policyBasicInfo);
        }
        if (null != respLDcomPojo) {
            BeanMapping.create(LDComPojo.class, PolicyBasicInfo.class).mapping(respLDcomPojo, policyBasicInfo);
            policyBasicInfo.setComName("农银人寿保险股份有限公司" + respLDcomPojo.getName());
        }

        if (respLCSpecList != null) {
            SpecialInfos specialInfos = new SpecialInfos();
            //特别约定
            List<SpecialInfo> specialInfoList = new ArrayList<>();
            for (int y = 0; y < respLCSpecList.size(); y++) {
                SpecialInfo specialInfo = new SpecialInfo();
                specialInfo.setSpecContent(respLCSpecList.get(y).getSpecContent());
                specialInfoList.add(specialInfo);
            }
            specialInfos.setSpecialInfos(specialInfoList);
            //特别约定
            policyBasicInfo.setSpecialInfos(specialInfos);
        }

        //组装险种信息
        Risks risks = new Risks();
        List<Risk> riskList = new ArrayList<>();

        if (null != respLCPolList) {
            for (int i = 0; i < respLCPolList.size(); i++) {
                Risk risk = new Risk();
                //险种编码
                risk.setRiskCode(respLCPolList.get(i).getRiskCode());
                if (null != respLCPolList.get(i).getPolNo()) {
                    if (respLCPolList.get(i).getPolNo().equals(respLCPolList.get(i).getMainPolNo())) {
                        risk.setMainRiskCode(respLCPolList.get(i).getRiskCode());
                        //保险终止日期
                        policyBasicInfo.setInsuEndDate(TimeUtil.dateString10To8(respLCPolList.get(i).getEndDate()));
                        //交至日期
                        policyBasicInfo.setPayEndDate(TimeUtil.dateString10To8(respLCPolList.get(i).getPayEndDate()));
                    } else {
                        risk.setMainRiskCode("0");
                    }
                }
                if (null != respLMRiskAppList) {
                    for (int j = 0; j < respLMRiskAppList.size(); j++) {
                        if (null != respLMRiskAppList.get(j).getRiskCode()) {
                            if (respLMRiskAppList.get(j).getRiskCode().equals(respLMRiskAppList.get(i).getRiskCode())) {
                                risk.setRiskName(respLMRiskAppList.get(j).getRiskName());
                            }
                        }
                    }
                }
                //缴费年期
                risk.setPayIntv(String.valueOf(respLCPolList.get(i).getPayIntv()));
                //缴费期间
                risk.setPayEndYear(String.valueOf(respLCPolList.get(i).getPayEndYear()));
                risk.setInsuYearFlag(respLCPolList.get(i).getInsuYearFlag());
                risk.setPayEndYearFlag(respLCPolList.get(i).getPayEndYearFlag());
                risk.setPrem(String.valueOf(respLCPolList.get(i).getPrem()));
                risk.setAmnt(String.valueOf(respLCPolList.get(i).getAmnt()));
                risk.setMult(String.valueOf(respLCPolList.get(i).getMult()));
                risk.setInsuYear(String.valueOf(respLCPolList.get(i).getInsuYear()));
                risk.setGetYear(String.valueOf(respLCPolList.get(i).getGetYear()));//养老金开始领取日GetYear周岁后的首个保单周年日
                risk.setLiveGetMode(respLCPolList.get(i).getLiveGetMode());//养老金领取方式
                risk.setGetIntv(respLCPolList.get(i).getGetForm());//领取频率
                risk.setBonusGetMode(respLCPolList.get(i).getBonusGetMode());//红利领取方式
                //1016返回所需要的字段
                msg.getBody().getRisk().setCValiDate(TimeUtil.dateString10To8(respLCPolList.get(i).getCValiDate()));
                msg.getBody().getRisk().setInsuEndDate(TimeUtil.dateString10To8(respLCPolList.get(i).getEndDate()));
                msg.getBody().getRisk().setMult(String.valueOf(respLCPolList.get(i).getMult()));
                msg.getBody().setAmnt(String.valueOf(respLCPolList.get(i).getAmnt()));
                //现金价值
                CashValues cashValues = new CashValues();
                List<CashValue> cashValueList = new ArrayList<>();

                if (null != respCashValueList) {
                    for (int z = 0; z < respCashValueList.size(); z++) {
                        if (null != respCashValueList.get(z).getRiskCode()) {
                            if (respCashValueList.get(z).getRiskCode().equals(respLCPolList.get(i).getRiskCode())) {
                                List list = respCashValueList.get(z).getCashValue();
                                if (null != list) {
                                    for (int x = 0; x < list.size(); x++) {
                                        CashValue cashValue = new CashValue();
                                        String resCashValue = (String) list.get(x);
                                        cashValue.setPolYear(resCashValue.split("_")[0]);
                                        cashValue.setValue(resCashValue.split("_")[1]);
                                        cashValueList.add(cashValue);
                                    }
                                }
                            }
                        }
                    }
                }
                if (cashValueList != null) {
                    cashValues.setCashValues(cashValueList);
                    risk.setCashValues(cashValues);
                }
                //减额交清保额
                ReducedVolumes reducedVolumes = new ReducedVolumes();
                List<ReducedVolume> reducedVolumeList = new ArrayList<>();

                if (null != respReducedVolumeList) {
                    for (int z = 0; z < respReducedVolumeList.size(); z++) {
                        if (null != respReducedVolumeList.get(z).getRiskCode()) {
                            if (respReducedVolumeList.get(z).getRiskCode().equals(respLCPolList.get(i).getRiskCode())) {
                                List list = respReducedVolumeList.get(z).getReducedVolume();
                                if (null != list) {
                                    for (int x = 0; x < list.size(); x++) {
                                        ReducedVolume reducedVolume = new ReducedVolume();
                                        String resReducedVolume = (String) list.get(x);
                                        reducedVolume.setPolYear(resReducedVolume.split("_")[0]);
                                        reducedVolume.setValue(resReducedVolume.split("_")[1]);
                                        reducedVolumeList.add(reducedVolume);
                                    }
                                }
                            }
                        }
                    }
                }
                if (reducedVolumeList != null) {
                    reducedVolumes.setReducedVolumes(reducedVolumeList);
                    risk.setReducedVolumes(reducedVolumes);
                }
                riskList.add(risk);
            }
        }
        risks.setRisks(riskList);
        msg.getBody().setPolicyBasicInfo(policyBasicInfo);
        msg.getBody().setRisks(risks);

        //组装打印信息
        //投保人
        Appnt appnt = new Appnt();
        if (null != respLCAppntPojo) {
            BeanMapping.create(LCAppntPojo.class, Appnt.class).mapping(respLCAppntPojo, appnt);
        }
        msg.getBody().setAppnt(appnt);

        //被保人
        Insureds insureds = new Insureds();
        if (null != respLCInsuredList) {
            List<Insured> insuredList = new ArrayList<>();
            for (int j = 0; j < respLCInsuredList.size(); j++) {
                Insured insured = new Insured();
                BeanMapping.create(LCInsuredPojo.class, Insured.class).mapping(respLCInsuredList.get(j), insured);
                insuredList.add(insured);
            }
            insureds.setInsureds(insuredList);
        }
        msg.getBody().setInsureds(insureds);

        //受益人
        Bnfs bnfs = new Bnfs();
        List<Bnf> bnfList = new ArrayList<>();
        boolean flag0 = false;// 当查询到生存受益人时候，那么标识不需要添加被保人为默认生存受益人。
        boolean flag1 = false;// 当查询到身故受益人时候，那么标识不需要添加法定为默认身故受益人
        if (null != respLCBnfList) {
            for (int z = 0; z < respLCBnfList.size(); z++) {
                String bnfType = respLCBnfList.get(z).getBnfType();
                if (bnfType.equals("0")) {
                    flag0 = true;
                } else if (bnfType.equals("1")) {
                    flag1 = true;
                }
                Bnf bnf = new Bnf();
                BeanMapping.create(LCBnfPojo.class, Bnf.class).mapping(respLCBnfList.get(z), bnf);
                bnf.setBnfRate(String.valueOf((int) (respLCBnfList.get(z).getBnfLot() * 100)));
                bnf.setBnfNo(String.valueOf(respLCBnfList.get(z).getBnfNo()));
                bnfList.add(bnf);
            }
        }
        if (!flag0) {
            if (null != respLCInsuredList) {
                for (int j = 0; j < respLCInsuredList.size(); j++) {
                    LCInsuredPojo lcInsuredPojo = respLCInsuredList.get(j);
                    Bnf bnf = new Bnf();
                    /** 受益人类型 */
                    bnf.setBnfType("0");// 生存受益人
                    /** 受益人姓名 */
                    bnf.setName(lcInsuredPojo.getName());
                    /** 受益人顺序号 */
                    bnf.setBnfNo("1");
                    /** 受益人级别 */
                    bnf.setBnfNo("1");
                    /** 受益人性别 */
                    bnf.setSex(lcInsuredPojo.getSex());
                    /** 受益人证件类型 */
                    bnf.setiDType(lcInsuredPojo.getIDType());
                    /** 受益人证件号 */
                    bnf.setiDNo(lcInsuredPojo.getIDNo());
                    /** 受益人出生日期 */
                    bnf.setBirthday(lcInsuredPojo.getBirthday());
                    /** 受益人份额 */
                    bnf.setBnfRate("100.0");
                    /** 受益人与被保险人关系 */
                    bnf.setRelaToInsured("00");
                    bnfList.add(bnf);
                }
            }
        }
        if (!flag1) {// 当没有在lcbnf中查询到受益人信息时。默认添加身故受益人
            // 设置身故受益人信息
            Bnf bnf = new Bnf();
            bnf.setName("法定");
            bnf.setBnfType("1");
            bnfList.add(bnf);
        }
        bnfs.setBnfs(bnfList);
        msg.getBody().setBnfs(bnfs);

        if (null != respLMRiskList) {
            LMRiskPojo responseLMRisk = respLMRiskList.get(0);
            BeanMapping.create(LMRiskPojo.class, Risk.class).mapping(responseLMRisk, msg.getBody().getRisk());
        }

        msg.getBody().setContValue(String.valueOf(respTradeInfo.getData("policyValue")));
        msg.getBody().setContState(String.valueOf(respTradeInfo.getData("policyStatus")));
        msg.getBody().setPledge(String.valueOf(respTradeInfo.getData("policyPledge")));
        msg.getBody().setInsuTime(String.valueOf(respTradeInfo.getData("insuTime")));
        msg.getHead().setFlag("0");

        return msg;
    }

    /**
     * 封装lKTransStatusPojo
     *
     * @return
     */
    public LKTransStatusPojo getLKTransStatus(Body body, Head head) {
        LKTransStatusPojo rLKTransStatus = new LKTransStatusPojo();

        BeanMapping.create(Body.class, LKTransStatusPojo.class).mapping(body, rLKTransStatus);
        BeanMapping.create(Head.class, LKTransStatusPojo.class).mapping(head, rLKTransStatus);

        String zoneNo = head.getZoneNo();
        String brNo = head.getNodeNo();

        rLKTransStatus.setTransDate(TimeUtil.dateString8To10(body.getOrgTransDate()));
        rLKTransStatus.setBankNode(brNo);
        rLKTransStatus.setBankBranch(zoneNo);

        return rLKTransStatus;
    }

    /**
     * 若在交易过程中出现问题，需要返回的错误报文信息
     *
     * @param msg
     * @return
     */
    public TranData getErrorTranData(TranData msg, String errorDesc) {
        //交易失败返回的头部信息
        msg.getHead().setFlag(ONE);
        msg.getHead().setDesc(errorDesc);

        //交易失败的体信息全部为空
        msg.setBody(new Body());
        msg.getBody().setRisk(new Risk());
        msg.getBody().setRisks(new Risks());
        msg.getBody().setPolicyBasicInfo(new PolicyBasicInfo());
        msg.getBody().getPolicyBasicInfo().setSpecialInfos(new SpecialInfos());
        msg.getBody().setAppnt(new Appnt());
        msg.getBody().setInsureds(new Insureds());
        msg.getBody().setBnfs(new Bnfs());

        cLogger.error(msg.getHead().getTransCode() + "交易结束！");
        return msg;
    }

    /**
     * 返回异常信息
     *
     * @param errorStr
     * @return
     */
    public TradeInfo getErrorTradeInfo(String errorStr) {
        TradeInfo tradeInfo = new TradeInfo();
        tradeInfo.addError(errorStr);
        return tradeInfo;
    }

    /**
     * 返回投保或者核保的异常信息，将异常信息返回给银行
     * 如果存在异常信息，则返回内部错误，交易失败
     *
     * @param errorStr
     * @return
     */
    public TradeInfo getCheckRuleFailInfo(String errorStr, String errorLog) {

        //若微服务返回信息含有Exception，则直接返回银行内部错误，交易失败
        if (errorStr.contains("Exception") || errorStr.contains("EXCEPTION") || errorStr.contains("exception")) {
            return getErrorTradeInfo(errorLog);
        }

        TradeInfo tradeInfo = new TradeInfo();
        tradeInfo.addData("return", errorStr);
        tradeInfo.addData("log", errorLog);
        return tradeInfo;
    }

    /**
     * 应对超时异常，传入微服务开始时间，结束时间，微服务名称，异常信息，统一处理
     *
     * @param startTime
     * @param endTime
     * @param serviceName
     * @return
     */
    public String getExceptionStr(long startTime, long endTime, String serviceName, Exception e) {

        StringBuffer buffer = new StringBuffer(1024);
        long dealTime = endTime - startTime;

        if (dealTime > accessControl.getReadTimeout()) {
            buffer.append("调用").append(serviceName).append("微服务超时，调用时间：").append(dealTime).append("ms")
                    .append(System.getProperty("line.separator")).append(ExceptionUtils.exceptionToString(e));
        } else {
            buffer.append("调用").append(serviceName).append("微服务异常。")
                    .append(System.getProperty("line.separator")).append(ExceptionUtils.exceptionToString(e));
        }

        return buffer.toString();
    }

    /**
     * 不同的交易实现类调用不同的微服务接口
     *
     * @param msg :参数是转换为pojo后的TradeInfo
     * @return 返回TradeInfo
     */
    public abstract TradeInfo service(TradeInfo msg);

}
